package com.kongge.algorithm.list;

public class ListNode<T> {
	public T t;
	public ListNode<T> next;
	
	public ListNode() {
	}
	
	public ListNode(T t) {
		this.t = t;
	}
	
	public static <T> ListNode<T> createList(T... arr) {
		if (arr == null || arr.length == 0) {
			return null;
		}
		ListNode<T> head = new ListNode<T>(arr[0]);
		ListNode<T> nextNode = head;
		for (int i = 1; i < arr.length; i++) {
			ListNode<T> node = new ListNode<T>(arr[i]);
			nextNode.next = node;
			nextNode = node;
		}
		return head;
	}
	
	public static <T> ListNode<T> get(ListNode<T> head, int position) {
		if (position < 0 || head == null) {
			return null;
		}
		ListNode<T> tempNode = head;
		int index = 0;
		while (tempNode != null) {
			if (index == position) {
				return tempNode;
			}
			index ++;
			tempNode = tempNode.next;
		}
		return null;
	}
	
	public static <T> void printList(ListNode<T> head) {
		if (head == null) {
			return;
		}
		ListNode<T> tempNode = head;
		while (tempNode != null) {
			System.out.print(" " + tempNode.t);
			tempNode = tempNode.next;
		}
	}
}
